﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Client.Managers
{
    class SettingManager
    {
        string _path = @"config\";
        string _userPath = Managers.Manager.userPath+ @"\userconfig\";
        string _fileName = "setting.ini";

        Dictionary<string, object> settings = new Dictionary<string, object>();
        public Dictionary<string, string> generalSetting { get; private set; }
        public Dictionary<string, string> voiceSetting { get; private set; }
        public Dictionary<string, string> fsSetting { get; private set; }
        public Dictionary<string, string> kSetting { get; private set; }

        public SettingManager()
        {
            generalSetting = new Dictionary<string, string>();
            voiceSetting = new Dictionary<string, string>();
            fsSetting = new Dictionary<string, string>();
            kSetting = new Dictionary<string, string>();

            LoadSetting();
            Manager.OnExit += SaveSetting;
        }

        void LoadSetting()
        {
            IniHelper.IniHelper defaultIni = new IniHelper.IniHelper(_path+_fileName);
            IniHelper.IniHelper userIni;
            if (File.Exists(_userPath+_fileName))
                userIni = new IniHelper.IniHelper(_userPath+_fileName);
            else
                userIni = defaultIni;

            //获取配置中每个section，根据section名称反射找到对应的dic，并添加数据
            var t = this.GetType();
            var l = userIni.ReadValues();
            var ld = defaultIni.ReadValues();
            //如果用户配置中没有包含默认配置项目，则添加。这样可以直接在默认配置中添加项目，可以自动填充到用户配置
            for(int i = 0; i < ld.Count; i++)
            {
                int k = 0;
                while (k < l.Count && ld[i].Section != l[k].Section) k++;
                if (k == l.Count)// user中没有包含当前默认项
                {
                    l.Add(ld[i]);
                    continue;
                }

                //user中包含默认项当前节点。便利节点条目，不包含则添加
                foreach(KeyValuePair<string,string> x in ld[i])
                {
                    bool exist = false;
                    foreach(KeyValuePair<string,string> y in l[k])
                    {
                        if (x.Key == y.Key)
                        {
                            exist = true;
                            break;
                        }
                    }
                    if (!exist)
                        l[k].Add(x.Key, x.Value, "");
                }
            }
            //找到对应的dic并填充
            foreach (var s in l)
            {
                //找到对应的dic
                var p = t.GetProperty(s.Section, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly);
                var dic = (Dictionary<string,string>)p.GetGetMethod().Invoke(this, null);
                //找到Add方法，添加数据
                var addM = dic.GetType().GetMethod("Add", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly);
                foreach(KeyValuePair<string,string> kv in s)
                {
                    string uv = userIni.ReadValue(kv.Key,s.Section);
                    if (uv != "")
                        addM.Invoke(dic, new object[] { kv.Key, uv });
                    else
                        addM.Invoke(dic, new object[] { kv.Key, kv.Value });
                }
                
                settings[s.Section] = dic;
            }
        }

        void SaveSetting()
        {
            if (!File.Exists(_userPath + _fileName))
            {
                if (!Directory.Exists(_userPath))
                    Directory.CreateDirectory(_userPath);
                using (File.Create(_userPath+_fileName)) ;
            }
            
            foreach(KeyValuePair<string ,object> kv in settings)
            {
                string section = kv.Key;
                Dictionary<string, string> set = (Dictionary<string, string>)kv.Value;
                foreach(KeyValuePair<string,string> i in set)
                {
                    IniHelper.IniHelper.Write(_userPath+_fileName, section, i.Key, i.Value, null);
                }
            }
        }

        private string GetValue(string key,Dictionary<string,string> dic)
        {
            if (dic.ContainsKey(key))
                return dic[key];
            foreach(var i in dic.Keys)
            {
            }
            throw new Exception("没有找到key为: " + key);
        }

        public string GetFSValue(string key)
        {
            return GetValue(key, fsSetting);
        }
        public string GetVoiceValue(string key)
        {
            return GetValue(key, voiceSetting);
        }
        public string GetGeneralValue(string key)
        {
            return GetValue(key, generalSetting);
        }
    }
}
